import './assets/main.css'

import { createApp } from 'vue'
import App from './App.vue'
//1 导入vue-router
import { createRouter, createWebHistory } from 'vue-router';
import BlogList from "@/components/BlogList.vue";
import Login from "../../../../apache-maven-3.9.9/SpringBoot/blog/src/components/Login.vue";
import {isAuthenticated} from "@/authService.js";
import Home from "@/components/Home.vue";
//2 配置路由
const routes = [
    {
        path: '/blogs',
        name: 'Blogs',
        component: BlogList,
        meta: { requiresAuth: true }, // 标记需要认证的路由
    },
    {
        path: '/',
        name: 'Home',
        component: Home,
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
    }
];
//创建一个路由器实例
const router = createRouter({
    history: createWebHistory(),
    routes,
});
// createApp(App).use(router).mount('#app');
const app = createApp(App);
//使用路由
app.use(router);
app.mount('#app');
//登陆验证
router.beforeEach((to, from, next) => {
    if (to.matched.some(record => record.meta.requiresAuth)) {
        if (!isAuthenticated()) {
            next({ name: 'Login' });
        } else {
            next();
        }
    } else {
        next();
    }
});
// export const isAuthenticated = () => {
//     const token = localStorage.getItem('jwtToken');
//     return !!token; // 如果有令牌，则认为已认证
// };